package adavance.client;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class ReceiveFile {
    public static void main(String[] args) {
        Socket s = null;
        try {
            s = new Socket("xxxxxx", 8001);
            System.out.println("建立了服务器的连接");
            InputStream in = s.getInputStream();
            //OutputStream out = s.getOutputStream();
            byte[] b = new byte[1024];
            int len = 0;
            FileOutputStream f = new FileOutputStream("dog.png");
            while ((len = in.read(b)) > 0) {
                f.write(b, 0, len);
            }
            f.flush();//关闭的时候自动flush
            f.close();
            System.out.println("接收完毕");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (s != null && !s.isClosed())
                    s.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
